package BIO;

import java.io.FileInputStream;
import java.io.IOException;

/**
 * @author:谢君臣
 * @Date:2021/7/24-9:49
 * @version:1.0
 * @function:
 */
public class FileReadTest {
    public static void main(String[] args) throws IOException {
        String path = "D:\\Study\\README.md";
        FileInputStream fileInputStream = new FileInputStream(path);
        byte[] b = new byte[3];
        fileInputStream.read(b, 0, b.length);
        String xxx = bytesToHexString(b);
        xxx = xxx.toUpperCase();
        System.out.println("头文件是：" + xxx);
        String ooo = checkType(xxx);
        System.out.println("后缀名是：" + ooo);


    }
    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder();
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }
    public static String checkType(String xxxx) {

        switch (xxxx) {
            case "FFD8FF": return "jpg";
            case "89504E": return "png";
            case "474946": return "jif";
            case "232323": return "md";
            default: return "0000";
        }
    }
}
